﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aj.Core;
using System.Data;

public partial class Manager_LanMuEdit : AuthPageBase
{
    protected int pid;
    protected int cid;
    protected string name;
    protected string content;
    protected int enableCache;
    protected int articleCid;
    protected string sourceUrl;
    protected string urlName;
    protected int sort;
    protected string picUrl;
    protected string picTitle;

    protected DataSet ds = null;

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        cid = QueryParamInt32("cid");
        try
        {
            if (cid > 0)
            {
                DataSet cds = CustomQuery.Category_GetCategoryById(cid);
                if (cds != null && cds.Tables.Count > 0 && cds.Tables[0].Rows.Count > 0)
                {
                    pid = cds.Tables[0].Rows[0]["C_Pid"].ToInt();
                    name = cds.Tables[0].Rows[0]["C_Name"].ToString();
                }

                ds = CustomQuery.LanMuInfo_GetLanMuInfoByCId(cid);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    name = ds.Tables[0].Rows[0]["LM_Name"].ToString();
                    content = ds.Tables[0].Rows[0]["LM_Content"].ToString();
                    enableCache = ds.Tables[0].Rows[0]["LM_EnableCache"].ToInt();
                    articleCid = ds.Tables[0].Rows[0]["LM_ArticleCid"].ToInt();
                    sourceUrl = ds.Tables[0].Rows[0]["LM_SourceUrl"].ToString();
                    urlName = ds.Tables[0].Rows[0]["LM_UrlName"].ToString();
                    sort = ds.Tables[0].Rows[0]["LM_Sort"].ToInt();
                    picUrl = ds.Tables[0].Rows[0]["LM_PicUrl"].ToString();
                    picTitle = ds.Tables[0].Rows[0]["LM_PicTitle"].ToString();

                    if (IsPost)
                    {
                        int rel = CustomQuery.LanMuInfo_Update(cid, FormParam("name"), FormParam("content"), FormParamInt32("enableCache"), FormParamInt32("articleCid"), FormParam("sourceUrl"), FormParam("urlName"), sort, FormParam("picUrl"), FormParam("picTitle"));
                        if (rel > 0)
                        {
                            Function.RemoveCacheObject("/cache/menu/" + pid);
                            Function.RemoveCacheObject("/cache/" + Utility.GetLanmuUrlNmae(pid) + "lanmu/" + urlName);
                            Page_GoTo("LanMuList.aspx?cid=" + pid);
                        }
                        else
                        {
                            MessageBox("修改失败！");
                        }
                    }
                }
                else
                {
                    if (IsPost)
                    {
                        sort = cid;// FormParamInt32("sort")
                        int rel = CustomQuery.LanMuInfo_Add(pid, cid, FormParam("name"), FormParam("content"), FormParamInt32("enableCache"), FormParamInt32("articleCid"), FormParam("sourceUrl"), FormParam("urlName"), sort, User.Identity.Name, FormParam("picUrl"), FormParam("picTitle"));
                        if (rel > 0)
                        {
                            Function.RemoveCacheObject("/cache/menu/" + pid);
                            Page_GoTo("LanMuList.aspx?cid=" + pid);
                        }
                        else
                        {
                            MessageBox("新建失败！");
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.WriteErrorLog(ex);
        }
    }
}